# coding=utf-8

import requests
import re
import uuid
import socket

mac_date = {
    'F8:94:C2:2A:7A:29' : '我的笔记本',
    '44:9E:F9:89:08:87' : '我的手机',
    '6C:B7:49:95:C8:58' : '爸的手机',
    'C8:F2:30:75:1D:AA' : '妈的手机',
    '50:46:5D:05:9E:0F' : '家里台式',
}



def get_host_ip():
    # 获取本机ip
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
 
    return ip

def get_mac_address(): 
    # 获取本机mac
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:] 
    return ":".join([mac[e:e+2] for e in range(0,11,2)])


s = requests.Session()

login_url = 'http://192.168.1.1/login.cgi'

login_params = {
	'username' : 'useradmin',
	'psd' : 'pgqs5',
    }

res = s.get(url = login_url, params = login_params)

user_url = 'http://192.168.1.1/ctstatslan_user.html'

res = s.get(url = user_url)

res.encoding = 'utf-8'
pattern = r'<tr><td>(.*)</td><td>(.*)</td><td>'

infos = re.findall(pattern = pattern, string = res.text)

host_number = len(infos)

if host_number < 1 or res.status_code != 200 :
    print('获取信息失败！')
    exit()

print(u"当前在线设备数：%d" % host_number)

# local_mac = get_mac_address()
# local_ip = get_host_ip()

for i,info in enumerate(infos):
    print(u'%d\t%s\t%s\t%s' % (i+1, info[0], info[1], str(mac_date.get(str(info[1]).upper()))))

# 退出登陆
loginOut_url = 'http://192.168.1.1/login.html'
res = s.get(url = loginOut_url)
res.encoding = 'utf-8'
s.close()